package com.captain.special.二叉树.简单;

import com.captain.leetcode.TreeNode;

/**
 * Des:
 * TODO 树的直径
 *
 * @author XL
 * @Date 2022/5/7 15:53
 */
public class 二叉树的直径543 {
    int max = Integer.MIN_VALUE;
    public int diameterOfBinaryTree(TreeNode root) {
        recur(root);
        //总的节点数 - 1 =  直径
        return max - 1;
    }

    public int recur(TreeNode node) {
        if (node == null) {
            return 0;
        }
        int l = recur(node.left);
        int r = recur(node.right);
        //节点数 = 当前节点深度 + 1
        max = Math.max(max, l + r + 1);
        return Math.max(l, r) + 1;
    }
}
